from typing import Optional

from keras.src.trainers.data_adapters.array_slicing import can_slice_array


#
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
        cur = dummy = ListNode()
        carry = 0
        while l1 or l2 or carry:
            carry += (l1.val if l1 else 0) +  (l2.val if l2 else 0)
            cur.next = ListNode(carry%10)
            carry //= 10
            cur = cur.next
            if l1 : l1 = l1.next
            if l2 : l2 = l2.next
        return dummy.next


class ListNode:
    def __init__(self,val=0,next=None):
        self.val = val
        self.next = next
class Solution:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode])->Optional[ListNode]:
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            carry+=(l1.val if l1 else 0)+(l2.val if l2 else 0)
            cur.next = ListNode(carry%10)
            carry//=10
            cur = cur.next
            if l1:l1=l1.next
            if l2:l2=l2.next
        return dummy.next


class ListNode:
    def __init__(self,val=0,next=None):
        self.val = val
        self.next=next
class Solutionn:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode
        carry=0
        while l1 or l2 or carry:
            carry+=(l1.va if l1 else 0)+(l2.val if l2 else 0)
            cur.next=ListNode(carry %10)
            carry//=10
            cur=cur.next
            if l1:l1 = l1.next
            if l2:l2 = l2.next
        return dummy.next


class Solution:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+ carry
            cur.next=ListNode(total%10)
            carry=total//10
            cur=cur.next
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next


class Solution:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+ carry
            cur.next=ListNode(total%10)
            carry=total//10
            cur=cur.next
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next


class Solution:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+ carry
            cur.next=ListNode(total%10)
            carry=total//10
            cur=cur.next
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next

class ListNode:
    def __init(self,val=0,next=None):
        self.val=val
        self.next=next
class Solution:
    def addtwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+carry
            cur.next=ListNode(total%10)
            cur=cur.next
            carry=total//10
            if l1 :
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next

class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next

class Solution:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        dummy=cur=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+carry
            cur.next=ListNode(total%10)
            cur=cur.next
            carry=total//10
            if l1 :
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next

class Listnode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next
class Solution:
    def addtwonumbers(self,l1:Optional[Listnode],l2:Optional[ListNode]):
        dummy=cur=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+carry
            cur.next=ListNode(total%10)
            cur=cur.next
            carry=total//10
            if l1 :
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next

class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next
class Solution:
    def addtwonumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        dummy=cur=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+carry
            cur.next=total%10
            cur=cur.next
            carry=total/10
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next








